package com.global.college.ui.activity;

import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.global.college.R;
import com.global.college.UserData;
import com.global.college.base.BaseActivity;
import com.global.college.constants.Constant;
import com.global.college.ui.bean.UploadHeaderModel;
import com.global.college.ui.bean.UserDataModel;
import com.global.college.ui.interfaces.IUserInfoView;
import com.global.college.ui.presenter.UserInfoPresenter;
import com.global.college.utils.CommonUtils;
import com.global.college.utils.HnDateUtils;
import com.global.college.utils.StringUtils;
import com.global.college.utils.ToastUtils;
import com.global.college.views.HnEditHeaderDialog;
import com.global.college.views.SexChangeDialog;
import com.global.college.widget.FrescoImageView;
import com.global.college.widget.picker.photo_picker.HnPhotoUtils;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 用户中心
 */
public class UserCenterActivity extends BaseActivity implements IUserInfoView {

    @BindView(R.id.fiv_avater)
    FrescoImageView fivAvater;
    @BindView(R.id.iv_change)
    ImageView ivChange;
    @BindView(R.id.tv_change_tip)
    TextView tvChangeTip;
    @BindView(R.id.tv_nickname)
    TextView tvNickname;
    @BindView(R.id.ll_edit_nickname)
    LinearLayout llEditNickname;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.ll_edit_sex)
    LinearLayout llEditSex;
    @BindView(R.id.tv_save)
    TextView tvSave;
    private UserInfoPresenter userInfoPresenter;
    private UserDataModel mUserDataModel;
    @Override
    public int getContentViewId() {
        return R.layout.activity_user_center;
    }

    @Override
    public void onCreateNew(Bundle savedInstanceState) {
        setShowTitleBar(true);
        setShowBack(true);
        setTitle("个人资料");
        userInfoPresenter = new UserInfoPresenter(this);
        userInfoPresenter.setUserInfoViewView(this);
    }

    @Override
    public void getInitData() {

//        if (StringUtils.isNotEmpty(userDataModel.getAvatar(), true)) {
//            fivAvater.setImageURI( userDataModel.getAvatar());
//        } else {
            fivAvater.setImageURI(Uri.parse("res:///" + R.mipmap.icon_user_my_header));
//        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        userInfoPresenter.getUserInfo();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.iv_change, R.id.ll_edit_nickname, R.id.ll_edit_sex})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_change:
                updateHeader();
                break;
            case R.id.ll_edit_nickname:
                Bundle bundle = new Bundle();
                bundle.putString(Constant.Key.NAME, mUserDataModel.getNickName());
                JugeAndOpenActivity(EditUserInfoActivity.class,bundle);//
                break;
            case R.id.ll_edit_sex:
                SexChangeDialog sexChangeDialog = new SexChangeDialog(UserCenterActivity.this, new SexChangeDialog.OnCallBack() {
                        @Override
                        public void callBack(int param) {
                            if (0 == param ||1 == param || 2 == param) {
                                userInfoPresenter.setUserInfo("","",String.valueOf(param),UserData.getInstance().getPhone());
                            }
                        }
                    });
                sexChangeDialog.show();
                sexChangeDialog.setCanceledOnTouchOutside(false);
                break;
        }
    }
    /**
     * 更新头像
     */
    public void updateHeader() {
        HnEditHeaderDialog mHeaderDialog = HnEditHeaderDialog.newInstance();
        mHeaderDialog.show(this.getSupportFragmentManager(), "header");
        mHeaderDialog.setOnImageCallBack(new HnEditHeaderDialog.OnImageCallBack() {
            @Override
            public void onImage(Bitmap bitmap, Uri uri) {
                if (bitmap != null) {
                    //对文件名进行MD5加密   YYYYMMDD +md5
                    String fileName = HnDateUtils.getCurrentDate("yyyyMMdd").toUpperCase() + CommonUtils.createRandom(false, 5) + ".png";
                    File picFile = HnPhotoUtils.bitmapToFile(UserCenterActivity.this,bitmap, fileName);
                    if (picFile.exists()) {
                        userInfoPresenter.uploadAvatar(picFile.getPath());
                        mHeaderDialog.dismiss();
                    }
                }
            }
        });
    }

    @Override
    public void onGetUserInfoSuccess(UserDataModel userDataModel) {
        UserData.getInstance().setUserModel(userDataModel);
        mUserDataModel = userDataModel;
        if (null != userDataModel){
            tvNickname.setText(StringUtils.isNotEmpty(userDataModel.getNickName(), true) ? userDataModel.getNickName() : "暂无");
            switch (userDataModel.getSex()){
                case "0":
                    tvSex.setText("男");
                    break;
                case "1":
                    tvSex.setText("女");
                    break;
                default:
                    tvSex.setText("保密");
                    break;
            }
            if (StringUtils.isNotEmpty(userDataModel.getAvatar(), true)) {
                fivAvater.setImageURI( userDataModel.getAvatar());
            } else {
                fivAvater.setImageURI(Uri.parse("res:///" + R.mipmap.icon_user_my_header));
            }
        }
    }

    @Override
    public void onGetUserInfoFail(String error) {

    }

    @Override
    public void onSetUserInfoSuccess(Object smsModel) {
        ToastUtils.showCenterToast("设置成功");
        userInfoPresenter.getUserInfo();
    }

    @Override
    public void onSetUserInfoFail(String error) {
    }

    @Override
    public void onUploadHeaderSuccess(UploadHeaderModel uploadHeaderModel) {
        userInfoPresenter.setUserInfo(uploadHeaderModel.getAvatar(),"","",UserData.getInstance().getPhone());
    }

    @Override
    public void onUploadHeaderFail(String error) {

    }
}